package com.lsw.jgyf.ui.activity;

import android.content.Intent;
import android.os.Build;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.lsw.jgyf.R;
import com.lsw.jgyf.adapter.PopupWindowSortAdapter;
import com.lsw.jgyf.model.BankCardModel;
import com.lsw.jgyf.model.CreateOrderModel;
import com.lsw.jgyf.model.FrontCardModel;
import com.lsw.jgyf.model.PopupWindowSortModel;
import com.lsw.jgyf.ui.base.activity.TitleBarActivity;
import com.lsw.jgyf.util.GlideUtils;
import com.lsw.photoview.TakePhotoActivity;
import com.lsw.util.AppManager;
import com.lsw.util.DensityUtil;
import com.lsw.util.LogUtils;
import com.lsw.util.StringUtil;
import com.lsw.util.ToastUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by lsw on 2018/7/12.
 */

public class CustomerInformationActivity extends TitleBarActivity<CustomerInformationPresenter> implements CustomerInformationContract.View, View.OnClickListener {
    @BindView(R.id.rl_zhenmian)
    RelativeLayout rl_zhenmian;
    @BindView(R.id.iv_Idzhengmian)
    ImageView iv_Idzhengmian;
    @BindView(R.id.rl_fanmian)
    RelativeLayout rl_fanmian;
    @BindView(R.id.iv_Idfanmian)
    ImageView iv_Idfanmian;
    @BindView(R.id.rl_bankcard)
    RelativeLayout rl_bankcard;
    @BindView(R.id.iv_bankcard)
    ImageView iv_bankcard;
    private String card_num = "";
    @BindView(R.id.ll_sex)
    LinearLayout ll_sex;
    @BindView(R.id.tv_sex)
    TextView tv_sex;
    @BindView(R.id.tv_phone)
    TextView tv_phone;
    @BindView(R.id.et_name)
    EditText et_name;
    @BindView(R.id.et_card_num)
    EditText et_card_num;
    @BindView(R.id.et_address)
    EditText et_address;
    @BindView(R.id.et_bankcard)
    EditText et_bankcard;
    @BindView(R.id.et_youxiaoqi)
    EditText et_youxiaoqi;
    @BindView(R.id.et_jigou)
    EditText et_jigou;
    @BindView(R.id.et_bank_name)
    EditText et_bank_name;
    @BindView(R.id.tv_buy)
    TextView tv_buy;

    String mobile, phone_id, memory, color, num, is_new, packageX, price, periods, insurance_id, company_id, company_product_id, first_money, buy_type, discount_price, package_expenses;
    String name, sex, id_number, address, issue_time, end_time, issue, bank_name, bank_card_number;

    @Override
    protected void initInjector() {
        mActivityComponent.inject(this);
    }

    @Override
    protected void initEventAndData() {
        mobile = getIntent().getStringExtra("mobile");
        phone_id = getIntent().getStringExtra("phone_id");
        memory = getIntent().getStringExtra("memory");
        color = getIntent().getStringExtra("color");
        num = getIntent().getStringExtra("num");
        is_new = getIntent().getStringExtra("is_new");
        packageX = getIntent().getStringExtra("package");
        price = getIntent().getStringExtra("price");
        periods = getIntent().getStringExtra("periods");
        insurance_id = getIntent().getStringExtra("insurance_id");
        company_id = getIntent().getStringExtra("company_id");
        company_product_id = getIntent().getStringExtra("company_product_id");
        first_money = getIntent().getStringExtra("first_money");
        buy_type = getIntent().getStringExtra("buy_type");
        discount_price = getIntent().getStringExtra("discount_price");
        package_expenses = getIntent().getStringExtra("package_expenses");


        rl_zhenmian.setOnClickListener(this);
        rl_fanmian.setOnClickListener(this);
        rl_bankcard.setOnClickListener(this);
        ll_sex.setOnClickListener(this);
        tv_buy.setOnClickListener(this);
        PopupWindowSortModel model = new PopupWindowSortModel();
        model.setName("男");
        model.setId("1");
        list1.add(model);
        PopupWindowSortModel model1 = new PopupWindowSortModel();
        model1.setName("女");
        model1.setId("1");
        list1.add(model1);
        tv_phone.setText(mobile);
    }

    @Override
    protected int setContentLayoutView() {
        return R.layout.activity_customer_information;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rl_zhenmian:
                TakePhotoActivity.startActivityForSiglePhoto(mContext, 1, false);
                break;
            case R.id.rl_fanmian:
                if (StringUtil.isNullOrEmpty(card_num)) {
                    ToastUtil.showToast(mContext, "请先上传身份证正面!");
                    return;
                }
                TakePhotoActivity.startActivityForSiglePhoto(mContext, 2, false);
                break;
            case R.id.rl_bankcard:
                TakePhotoActivity.startActivityForSiglePhoto(mContext, 3, false);
                break;
            case R.id.ll_sex:
                initPopuptWindow(list1, ll_sex);
                break;
            case R.id.tv_buy:
                name = et_name.getText().toString().trim();
                sex = tv_sex.getText().toString().trim();
                id_number = et_card_num.getText().toString().trim();
                address = et_address.getText().toString().trim();
                issue = et_jigou.getText().toString().trim();
                bank_name = et_bank_name.getText().toString().trim();
                bank_card_number = et_bankcard.getText().toString().trim();
                mPresenter.CreateOrder(phone_id, memory, color, num, is_new, mobile, packageX, price, periods, insurance_id, company_id, company_product_id, first_money, name, sex, id_number, address, issue_time, end_time, issue, bank_name, bank_card_number, buy_type, discount_price, package_expenses);
                break;
        }
    }

    @Override
    public void setData() {

    }

    @Override
    public void setBackCard(FrontCardModel model) {
        if (model != null) {
            et_youxiaoqi.setText(model.getIssue_time() + "~" + model.getEnd_time());
            et_jigou.setText(model.getIssue());
            issue_time = model.getIssue_time();
            end_time = model.getEnd_time();
        }
    }

    @Override
    public void setBankcard(BankCardModel model) {
        if (model != null) {
            et_bank_name.setText(model.getBank_name());
            et_bankcard.setText(model.getBank_card_number());
        }
    }

    @Override
    public void setFront(FrontCardModel model) {
        if (model != null) {
            card_num = model.getCard_num();
            et_name.setText(model.getName());
            et_card_num.setText(card_num);
            et_address.setText(model.getAddress());
            tv_sex.setText(model.getSex());
        }
    }

    @Override
    public void CreateOrderSucceed(CreateOrderModel model) {
        AppManager.getAppManager().finishActivity(OrderFillingActivity.class);
        finish();
    }

    @Override
    protected void setActionBarDetail() {

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case 1:
                    String path = TakePhotoActivity.getDataPath(data);
                    LogUtils.i("lsw", "path===" + path);
                    if (!StringUtil.isNullOrEmpty(path)) {
                        Glide.with(mContext).load(path)
                                .asBitmap()
                                .placeholder(R.mipmap.muti_default_pic)
                                .into(iv_Idzhengmian);
                        mPresenter.UpFrontCard(path);
                    }
                    break;
                case 2:
                    String path1 = TakePhotoActivity.getDataPath(data);
                    LogUtils.i("lsw", "path1===" + path1);
                    if (!StringUtil.isNullOrEmpty(path1)) {
                        Glide.with(mContext).load(path1)
                                .asBitmap()
                                .placeholder(R.mipmap.muti_default_pic)
                                .into(iv_Idfanmian);
                        mPresenter.UpBackCard(card_num, path1);
                    }
                    break;
                case 3:
                    String path2 = TakePhotoActivity.getDataPath(data);
                    LogUtils.i("lsw", "path2===" + path2);
                    if (!StringUtil.isNullOrEmpty(path2)) {
                        Glide.with(mContext).load(path2)
                                .asBitmap()
                                .placeholder(R.mipmap.muti_default_pic)
                                .into(iv_bankcard);
                        mPresenter.UpBankcard(path2);
                    }
                    break;
            }
        }
    }

    PopupWindow popupWindow;
    private List<PopupWindowSortModel> list1 = new ArrayList<>();
    PopupWindowSortAdapter popupWindowSortAdapter;
    View popupWindow_view;
    ListView listView;

    /**
     * 创建PopupWindow
     */
    protected void initPopuptWindow(List<PopupWindowSortModel> list1, View view_bottom) {
        // TODO Auto-generated method stub
        // 获取自定义布局文件activity_popupwindow_left.xml的视图
        popupWindow_view = LayoutInflater.from(mContext).inflate(R.layout.popupwindow_sort, null,
                false);
        listView = popupWindow_view.findViewById(R.id.listview);
        popupWindowSortAdapter = new PopupWindowSortAdapter(mContext);
        popupWindowSortAdapter.setData(list1);
        listView.setAdapter(popupWindowSortAdapter);
        listView.setOnItemClickListener((adapterView, view, i, l) -> {
            PopupWindowSortModel item = popupWindowSortAdapter.getItem(i);
            tv_sex.setText(item.getName());
            popupWindow.dismiss();
        });
//        }
        popupWindow = new PopupWindow(popupWindow_view, DensityUtil.getWidth(mContext) - DensityUtil.dip2px(mContext, 150), LinearLayout.LayoutParams.WRAP_CONTENT, true);
//        // 设置动画效果
//        popupWindow.setAnimationStyle(R.style.AnimationFade);
        // 点击其他地方消失
        if (Build.VERSION.SDK_INT < 24) {
            popupWindow.showAsDropDown(view_bottom);
        } else {
            // 适配 android 7.0
            int[] location = new int[2];
            view_bottom.getLocationOnScreen(location);
            int x = location[0];
            int y = location[1];
            popupWindow.showAtLocation(view_bottom, Gravity.NO_GRAVITY, DensityUtil.dip2px(mContext, 110), y + view_bottom.getHeight() + DensityUtil.dip2px(mContext, 3));
        }
        popupWindow.setOnDismissListener(() -> {
            popupWindow = null;
        });
    }

    @Override
    public void openlogin() {
        OpenLogin();
    }
}
